<template>
  <div>
    <el-card class='box-card' shadow='always'>
      <div slot='header' class='clearfix'>
        <b>
          <svg-icon icon-class='menu' />
          角色列表</b
        >
      </div>
      <el-form ref='queryForm' :inline='true' :model='queryParams' size='mini'>
        <el-form-item>
          <el-button v-has-permission="['sys:role:add']" icon='el-icon-plus' type='success' @click='handleAdd'>新增
          </el-button>
        </el-form-item>
        <el-form-item prop='name'>
          <el-input
            v-model='queryParams.name'
            clearable
            placeholder='角色名称'
            @keyup.enter.native='handleQuery'
          />
        </el-form-item>
        <el-form-item>
          <el-button
            icon='el-icon-search'
            plain
            type='primary'
            @click='handleQuery'>搜索
          </el-button>
        </el-form-item>
      </el-form>

      <el-table
        ref='roleTable'
        v-loading='loading'
        :data='pageList'
        border
        highlight-current-row
        @selection-change='handleSelectionChange'
        @row-click='handleRowClick'>
        <el-table-column align='center' width='55'>
          <template slot-scope='scope'>
            <el-radio v-model='checkedRoleId' :label='scope.row.id'><span></span></el-radio>
          </template>
        </el-table-column>

        <el-table-column label='角色名称' prop='displayName' />
        <el-table-column label='角色编码' prop='roleName' />
        <el-table-column align='center' label='操作' width='100'>
          <template slot-scope='scope'>
            <el-button
              v-has-permission="['sys:role:edit']"
              circle
              icon='el-icon-edit'
              plain
              size='mini'
              type='primary'
              @click.stop='handleEdit(scope.row)'
            />
            <el-button
              v-has-permission="['sys:role:delete']"
              circle
              icon='el-icon-delete'
              plain
              size='mini'
              type='danger'
              @click.stop='handleDelete(scope.row)'
            />
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show='pagination.total > 0'
        :limit.sync='pagination.limit'
        :page.sync='pagination.page'
        :total='pagination.total'
        @pagination='handleQuery'
      />

      <!-- 角色弹窗 -->
      <el-dialog
        :title='dialog.title'
        :visible.sync='dialog.visible'
        width='450px'
      >
        <el-form
          ref='form'
          :model='form'
          :rules='rules'
          label-width='80px'
          @close='closeDialog'
        >
          <el-form-item label='角色名称' prop='displayName'>
            <el-input v-model='form.displayName' placeholder='请输入角色名称' />
          </el-form-item>

          <el-form-item label='角色编码' prop='roleName'>
            <el-input v-model='form.roleName' placeholder='请输入角色编码' />
          </el-form-item>

          <el-form-item label='排序' prop='sort'>
            <el-input-number
              v-model='form.sort'
              :min='0'
              controls-position='right'
              style='width: 100px'
            />
          </el-form-item>

          <el-form-item label='状态'>
            <el-radio-group v-model='form.status'>
              <el-radio :label='true'>正常</el-radio>
              <el-radio :label='false'>停用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
        <div slot='footer' class='dialog-footer'>
          <el-button type='primary' @click='handleSubmit'>确 定</el-button>
          <el-button @click='closeDialog'>取 消</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { add, del, list, update } from '@/api/admin/role'

export default {
  data() {
    return {
      checkedRoleId: undefined,
      forbidden: true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      queryParams: {
        queryMode: 'page',
        name: undefined
      },
      pagination: {
        page: 1,
        limit: 10,
        total: 0
      },
      pageList: [],
      dialog: {
        title: undefined,
        visible: false
      },
      // 表单参数
      form: {
        sort: 1,
        status: true
      },
      // 表单校验
      rules: {
        displayName: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ],
        roleName: [
          { required: true, message: '角色编码不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  async created() {
    this.handleQuery()
  },
  methods: {
    handleQuery() {
      this.queryParams.pageNum = this.pagination.page
      this.queryParams.pageSize = this.pagination.limit
      list(this.queryParams).then(response => {
        const { data } = response
        this.pageList = data.list
        this.pagination.total = data.totalCount
        this.loading = false
      })
    },
    handleReset() {
      this.pagination = {
        page: 1,
        limit: 10,
        total: 0
      }
      ;(this.queryParams.name = undefined), this.handleQuery()
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    handleAdd() {
      this.resetForm()
      this.dialog = {
        title: '新增角色',
        visible: true
      }
    },
    handleEdit(row) {
      this.resetForm()
      this.dialog = {
        title: '修改角色',
        visible: true
      }
      this.form = row
    },
    handleDelete(row) {
      const ids = row.id ? [row.id] : this.ids
      this.$confirm('确认删除已选中的数据项？', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          del(ids).then(() => {
            this.$message.success('删除成功')
            this.handleQuery()
          })
        })
        .catch(() => this.$message.info('已取消删除'))
    },
    handleSubmit: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          const id = this.form.id
          if (id !== undefined) {
            update(this.form).then(() => {
              this.$message.success('修改成功')
              this.dialog.visible = false
              this.handleQuery()
            })
          } else {
            add(this.form).then(() => {
              this.$message.success('新增成功')
              this.dialog.visible = false
              this.handleQuery()
            })
          }
        }
      })
    },
    resetForm() {
      this.form = {
        sort: 1,
        status: 1
      }
    },
    closeDialog() {
      this.resetForm()
      this.dialog = {
        title: undefined,
        visible: false
      }
    },
    handleRowClick(row) {
      this.checkedRoleId = row.id // 勾选行
      const currentRow = JSON.parse(JSON.stringify(row))
      this.$emit('roleClick', currentRow)
    }
  }
}
</script>
